<template>
  <div>
    <a-card :bordered="false" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="5" :sm="12">
              <a-form-item label="菜单名称">
                <a-input placeholder="请输入" v-model="queryParam.menuName"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="5">
              <a-form-item label="状态">
                <a-select placeholder="请选择" v-model="queryParam.status">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="1">显示</a-select-option>
                  <a-select-option value="0">隐藏</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="3" :sm="12">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" @click="this.fetch" icon="search"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 30px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
        <a-button v-if="menuAddEnable" type="primary" shape="round" icon="plus-circle" @click="$refs.drawer.add()">添加</a-button>
      </div>
      <a-table
        ref="table"
        rowKey="id"
        size="middle"
        :pagination="pagination"
        :loading="loading"
        :columns="columns"
        :dataSource="data">

        <span slot="menuType" slot-scope="text">
          {{ text | menuTypeFilter }}
        </span>
        <span slot="icon" slot-scope="text">
          <a-icon :type="text"/>
        </span>
        <span slot="hidden" slot-scope="text">
          <a-badge :status="text | hiddenTypeFilter" :text="text | hiddenFilter"/>
        </span>
        <span slot="hideChildren" slot-scope="text">
          <a-badge :status="text | hiddenTypeFilter" :text="text | hiddenFilter"/>
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter"/>
        </span>
        <span slot="customActionTitle"><a-icon type="form" :style="{ fontSize: '16px', color: '#4F99FF' }"/>  操作</span>
        <span slot="action" slot-scope="text, record">
          <a-dropdown :trigger="['click']">
            <a-menu slot="overlay" v-if="menuEditEnable || menuAddEnable || menuRemoveEnable">
              <a-menu-item v-show="menuEditEnable"><a @click="$refs.drawer.edit(record)"><a-icon type="edit" theme="filled"/> 编辑</a></a-menu-item>
              <a-menu-item v-show="menuRemoveEnable"><a @click="onClick([record.id])"><a-icon type="delete" theme="filled"/> 删除</a></a-menu-item>
            </a-menu>
            <a> <a-button type="dashed" icon="bars"/></a>
          </a-dropdown>
        </span>
        <span slot="perms" slot-scope="text">
          {{ text === '' ? '-' : text }}
        </span>
      </a-table>
      <menu-modal ref="drawer" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { checkPermission } from '@/utils/permissions'
import { findMenuList, delMenu } from '@/api/system'
import { handleTree } from '@/utils/util'
import MenuModal from '../modules/MenuModal'
import message from 'ant-design-vue/es/message'
const hiddenMap = {
    0: {
      status: 'success',
      text: '显示'
    },
    1: {
      status: 'error',
      text: '隐藏'
    },
    2: {
      status: 'default',
      text: '  —'
    }
}
const statusMap = {
  1: {
    status: 'success',
    text: '显示'
  },
  0: {
    status: 'error',
    text: '隐藏'
  }
}
export default {
  name: 'MenuList',
  components: {
    MenuModal
  },
  data () {
    return {
      visible: false,
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      hiddenSearch: false,
      icon: 'eye-invisible',
      // 表头
      columns: [
        {
          title: '菜单名称',
          align: 'center',
          dataIndex: 'name',
          width: '160px'
        },
        {
          title: '组件路径',
          align: 'center',
          dataIndex: 'component',
          width: '120px'
        },
        {
          title: '图标',
          align: 'center',
          dataIndex: 'icon',
          scopedSlots: { customRender: 'icon' },
          width: '100px'
        },
        {
          title: '排序',
          align: 'center',
          dataIndex: 'orderNum',
          width: '100px'
        },
        {
          title: '按钮类型',
          align: 'center',
          dataIndex: 'menuType',
          scopedSlots: { customRender: 'menuType' },
          width: '100px'
        },
        {
          title: '隐藏菜单',
          align: 'center',
          dataIndex: 'hidden',
          scopedSlots: { customRender: 'hidden' },
          width: '100px'
        },
        {
          title: '隐藏子菜单',
          align: 'center',
          dataIndex: 'hideChildren',
          scopedSlots: { customRender: 'hidden' },
          width: '120px'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          width: '80px'
        },
        {
          title: '权限标识',
          dataIndex: 'perms',
          width: '190px',
          align: 'center',
          scopedSlots: { customRender: 'perms' }
        },
        {
          width: '80px',
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          slots: { title: 'customActionTitle' }
        }
      ],
      data: [],
      pagination: false,
      loading: false,
      menuAddEnable: checkPermission('system:menu:add'),
      menuEditEnable: checkPermission('system:menu:edit'),
      menuRemoveEnable: checkPermission('system:menu:remove')
    }
  },
  created () {
    this.fetch()
  },
  filters: {
    menuTypeFilter (type) {
      const menuMap = {
        'M': '目录',
        'F': '按钮',
        'C': '菜单'
      }
      return menuMap[type]
    },
    hiddenFilter (type) {
      return hiddenMap[type].text
    },
    hiddenTypeFilter (type) {
      return hiddenMap[type].status
    },
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    handleOk () {
      this.fetch()
    },
    fetch () {
      this.loading = true
      findMenuList(Object.assign(this.queryParam)).then(res => {
        this.data = handleTree(res.data, 'id')
        this.loading = false
      })
    },
    onClick (id) {
      const self = this
      this.$confirm({
        title: '你确定删除该菜单吗?',
        content: h => <div style="color:red;">删除父级菜单其对应子级也会删除</div>,
        onOk () {
          delMenu({ menuId: id }).then(res => {
            self.fetch()
            message.success('菜单删除成功')
          }).catch(res => {
            message.error(((res.response || {}).data || {}).message || res.message)
          })
        },
        onCancel () {
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
